<template>
    <div>
        <el-form ref="form" :model="obj.data" label-width="100px">
            <!-- <el-form-item label="工单编号" prop="code" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.code" clearable autocomplete="off"></el-input>
            </el-form-item> -->
            <el-form-item label="任务主题" prop="title" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.title" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="工作内容" prop="msg" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.msg" clearable autocomplete="off" type="textarea" rows="5"></el-input>
            </el-form-item>
            <el-form-item label="起止时间">
                <DateRange v-model:start="obj.data.starttime" v-model:end="obj.data.endtime" sph="起始时间" eph="结束时间"
                type="datetimerange" format="YYYY-MM-DD HH:mm" valueformat="YYYY-MM-DD HH:mm" :width="330" :disabledDate="setDate" />
            </el-form-item>
            <el-form-item label="选择执行人" prop="userid" :rules="[formRulesMixin.notNull]">
                <el-select v-model="obj.data.userid" placeholder="选择保洁" class="search_select" clearable>
                    <el-option v-for="(item,index) in obj.property_all" :key="index" :label="item.name" :value="item.property_id" />
                </el-select>
            </el-form-item>
            <el-form-item label="优先级" prop="priority" :rules="[formRulesMixin.notNull]">
                <el-select v-model="obj.data.priority" placeholder="选择优先级" class="search_select" clearable>
                    <el-option v-for="(item,index) in globalData.priorityList" :key="index" :label="item.text" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item class="dialog-btns-item">
                <div class="dialog-btns">
                    <span class="btn clear" @click="$emit('close')">取消</span>
                    <span class="btn sub ml10" @click.prevent="upData">提交</span>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRaw,inject } from 'vue'
    import DateRange from '@/components/date_range.vue'
    let { proxy } = getCurrentInstance()
    let obj = reactive({
        data:{
            work_id: inject("id").value || 0,
            // code: "",
            title: "",
            msg:"",
            starttime:"",
            endtime: "",
            userid:"",
            priority:"",
        },
        property_all:[],
    })

    let setDate = d => { return d <= proxy.$utils.dateAddDay(new Date(), -1) }
    let init = async () => {
        obj.property_all = await proxy.$utils.getProperty(proxy, 1) //1表示保洁
        if(obj.data.work_id > 0){
            proxy.$ajax({
                url: 'cleaning/get',
                data: obj.data,
                bfLoading: true
            }).then(d => {
                if(d.success){
                    obj.data = {...obj.data, ...d.msg}
                    obj.data.priority = parseInt(obj.data.priority)
                }else{
                    proxy.elMessageMixin(d.err, 'error')
                }
            }).catch(() => { })
        }
    }
    init()
    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                proxy.$ajax({
                    url: 'cleaning/design',
                    data: obj.data,
                    bfLoading: true,
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin(`${obj.data.work_id>0?'修改':"添加"}成功`, 'success')
                        proxy.$emit('init')
                        proxy.$emit('close')
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }else{
                return false
            }
        })
    }
</script>

<style scoped>
</style>